<?php
namespace Cli\Controller;
use Think\Controller;
class IndexController extends Controller {
	public $agent = array();
	public $header = array();
    // 按日期采集数据入库
    public function get_article_date_list(){
		$start_date = $_GET['start_date'] ? $_GET['start_date'] : '2008-10-29';
		$end_date   = $_GET['end_date'] ? $_GET['end_date'] : date('Y-m-d');
		$cur_date   = $start_date;
		$start_time = time();
		$this->total_num = 0;
		$this->db_write_num = 0;

    	// var_dump($start_date);
    	// var_dump($end_date);
    	// exit;
    	while (true) {
    		if($cur_date >= $end_date){
    			exit('timeout');
    		}
    		$kw_list = $this->get_list_kw($cur_date);
    		$this->logs('获取[' . $cur_date . ']关键字清单：' . count($kw_list));
    		foreach ($kw_list as $rs) {
    			$this->get_article_list($cur_date, $rs['Key']);
    			$this->logs('当前采集数量：' . $this->cur_total_num);
    			$this->logs('总采集数量：' . $this->total_num);
    			$this->logs('总入库数量：' . $this->db_write_num);
    			$this->logs('总采集时间：' . (time() - $start_time) . 's');
    		}
    		// 没有关键字处理
    		if(empty($kw_list)){
    			$this->get_article_list($cur_date);
    			$this->logs('当前采集数量：' . $this->cur_total_num);
    			$this->logs('总采集数量：' . $this->total_num);
    			$this->logs('总入库数量：' . $this->db_write_num);
    			$this->logs('总采集时间：' . (time() - $start_time) . 's');
    		}
    		$cur_date = date('Y-m-d', strtotime('+1 day', strtotime($cur_date)));
    	}
    }
    // 更新文章内容
    public function update_article_detail($pagesize = 10){
		$where['is_get_detail']  = 0;
		$where['get_detail_num'] = array('lt', 3);
    	$count = D('CourtArticle')->where($where)->count();
		$list  = D('CourtArticle')->field('doc_id,get_detail_num')->where($where)->limit($pagesize)->select();
		if(!$list){
			$this->logs('没有数据！');
			return false;
		}
		$this->logs('需要更新数据有' . $count . '条');
		foreach ($list as $rs) {
			$data = array();
			$row  = array();
			$row  = $this->get_article_detail($rs['doc_id']);
			if($row['PubDate']){
				$data['pub_date'] = $row['PubDate'];
			}
			if($row['Html']){
				$data['content_html'] = $row['Html'];
			}
			if(empty($data) && $rs['get_detail_num'] < 3){
				$this->logs('获取不到数据->' . $rs['doc_id'] . '[' . $rs['get_detail_num'] . ']');
				D('CourtArticle')->where(array('doc_id' => $rs['doc_id']))->setInc('get_detail_num');
				continue;
			}
			$data['is_get_detail'] = 1;
			$data['update_time']   = time();
			if(D('CourtArticle')->where(array('doc_id' => $rs['doc_id']))->save($data)){
				$this->logs('更新数据成功->' . $rs['doc_id']);
			}else{
				$this->logs('更新数据失败->' . $rs['doc_id']);
			}
		}
		$this->update_article_detail($pagesize);
    }
    // 抓取文章列表
    public function get_article_list($date = '', $keyword = '', $page = 1){
    	$date = $date ? $date : date('Y-m-d');
    	$this->logs('当前采集页数：' . $page);
    	// 页数大于25的，取消采集
    	if($page > 25){
    		return false;
    	}
    	$url = "http://www.court.gov.cn/zgcpwsw/List/ListContent";
		$params['Index'] = $page;
		$params['Page']  = 20;
		$params['Param'] = '裁判日期:' . $date . ' TO ' . $date . ($keyword ? ',关键词:' . $keyword : '');
		$content = http($url, $params, 'POST', '', $this->agent);
		$content = str_replace(
			array('"[{', '}]"', '\\\\', '\"'), 
			array('[{', '}]', '\\', '"'),
			$content
		);
    	$this->logs('采集参数：' . print_r($params, true));

		// var_dump($content);
		$json_data = json_decode($content, true);
		$count = $json_data[0]['Count'];
		$pagesize   = $params['Page'];
		$total_page = ceil($count / $pagesize);
		unset($json_data[0]);
		$list = $json_data;
		$this->cur_total_num = $count;
		$this->total_num += count($list);

		// var_dump($count);
		// var_dump($total_page);
		// var_dump($list);
		// 入库操作
		$this->list2db($list);
		// 继续采集当前日期数据
		if($page < $total_page){
			$page++;
			$this->get_article_list($date, $keyword, $page);
		}
    }
    // 采集列表数据入库
    public function list2db($list){
    	if(empty($list)){
    		$this->logs('没有数据！');
    		return false;
    	}
    	foreach ($list as $rs) {
    		$row = $this->list2db_feild($rs);
			$data[]   = $row;
			$doc_id[] = $row['doc_id'];
    	}
    	$new_data = array();
    	$have_doc_id = $this->is_have_doc_id(implode(',', $doc_id));
    	foreach ($data as $rs) {
    		if(!in_array($rs['doc_id'], $have_doc_id)){
				$db_doc_id[] = $rs['doc_id'];
				$new_data[]  = $rs;
    		}
    	}
    	if(empty($new_data)){
    		$this->logs('没有数据！');
    		return false;
    	}
    	$this->logs('当前获取DOCID：' . print_r($doc_id, true));
    	$this->logs('当前入库DOCID：' . print_r($db_doc_id, true));
    	// $this->logs('当前入库数据：' . print_r($new_data, true));
    	if(D('CourtArticle')->addAll($new_data)){
    		$this->logs('入库成功->' . count($new_data) . '条');
    		$this->db_write_num += count($new_data);
    	}else{
    		$this->logs('当前入库数据：' . print_r($new_data, true));
    	}
    }
    public function list2db_feild($data = ''){
    	// 裁判要旨段原文
    	// DocContent
    	// 案件类型
    	// 裁判日期
    	// 案件名称
    	// 文书ID
    	// 审判程序
    	// 案号
    	// 法院名称
		$new_data['title']         = $data['案件名称'] ? $data['案件名称'] : '';
		$new_data['judgment_date'] = $data['裁判日期'] ? $data['裁判日期'] : '';
		$new_data['doc_id']        = $data['文书ID'] ? $data['文书ID'] : '';
		$new_data['proceedings']   = $data['审判程序'] ? $data['审判程序'] : '';
		$new_data['case_no']       = $data['案号'] ? $data['案号'] : '';
		$new_data['court_name']    = $data['法院名称'] ? $data['法院名称'] : '';
		$new_data['reason_desc']   = $data['裁判要旨段原文'] ? $data['裁判要旨段原文'] : '';
		$new_data['content_text']  = $data['DocContent'] ? $data['DocContent'] : '';
		$new_data['create_time']   = time();
		if(!\Org\Util\String::isUtf8($new_data['content_text'])){
			$new_data['content_text'] = \Org\Util\String::autoCharset($new_data['content_text']);
		}
		return $new_data;
    }
    // 是否有文书ID
    public function is_have_doc_id($doc_id = ''){
    	$where['doc_id'] = array('in', $doc_id);
    	$doc_id_arr = D('CourtArticle')->where($where)->getField('doc_id', true);

    	return $doc_id_arr ? $doc_id_arr : false;
    }
	// 抓取文章详情
    public function get_article_detail($doc_id = ''){
    	$doc_id = '380d38b0-fd67-498d-aab7-ccba9b0fa090';
    	$type = substr($doc_id, 0, 4);
		$url = "http://www.court.gov.cn/zgcpwsw/Html_Pages/$type/$doc_id.js";
		// var_dump($this->agent);exit;
		// var_dump($url);
		$js_content  = http($url, $params, '', '', $this->agent);
		var_dump($js_content);exit;
		preg_match("/var jsonHtmlData = \"(.*?)\"}\";/si", $js_content, $content);
		$new_content = str_replace(
			array('\"', 'Title:', 'PubDate:', 'Html:'),
			array('"', '"Title":', '"PubDate":', '"Html":'),
			$content[1]
		);
		$json_content = substr($new_content, 0, -1) . '"}';
		$json_content = str_replace('\\', '\\\\', $json_content);
		$data = json_decode($json_content, true);
		// var_dump($new_content);
		// var_dump($json_content);
		// var_dump($data);exit;
		return $data;
    }
    // 获取列表关键字数据
    public function get_list_kw($date = ''){
    	// $date = '2016-02-01';
		$date = $date ? $date : date('Y-m-d');
		$url  = 'http://www.court.gov.cn/zgcpwsw/List/TreeContent';
		$params['Param'] = '裁判日期:' . $date . ' TO ' . $date;
		$content = http($url, $params, 'POST', '', $this->agent);
		$content = str_replace(
			array('"[{', '}]"', '\\\\', '\"'), 
			array('[{', '}]', '\\', '"'),
			$content
		);
		$data = json_decode($content, true);
		$kw_data = $data[0]['Child'];

		return $kw_data;
    }
    //写入日志
	protected function logs($content){
		echo $content . "\r\n";
		\Think\Log::write($content, \Think\Log::INFO, '');
	}
}





